<?php
/**
 * 消息管理控制器
 * 作者: 王勇程 (laughskydragon@qq.com)
 */

namespace app\admin\controller;

use app\admin\controller\AdminBase;

class Message extends AdminBase
{
    /**
     * 消息列表
     */
    public function index()
    {
        $where = $this->logicMessage->getWhere($this->param);
        
        $list = $this->logicMessage->getMessageList($where, true, 'id desc', DB_LIST_ROWS);
        
        $this->assign('list', $list);
        
        return $this->fetch();
    }
    
    /**
     * 消息详情
     */
    public function detail()
    {
        $id = $this->param['id'] ?? 0;
        
        if (!$id) {
            $this->jump([RESULT_ERROR, '参数错误']);
        }
        
        $info = $this->logicMessage->getMessageInfo(['id' => $id]);
        
        if (!$info) {
            $this->jump([RESULT_ERROR, '消息不存在']);
        }
        
        $this->assign('info', $info);
        
        return $this->fetch();
    }
    
    /**
     * 发送消息
     */
    public function send()
    {
        IS_POST && $this->jump($this->logicMessage->sendMessage($this->param));
        
        return $this->fetch();
    }
    
    /**
     * 批量发送消息
     */
    public function batchSend()
    {
        IS_POST && $this->jump($this->logicMessage->sendBatchMessage($this->param));
        
        return $this->fetch();
    }
    
    /**
     * 删除消息
     */
    public function delete()
    {
        $this->jump($this->logicMessage->deleteMessage($this->param));
    }
    
    /**
     * 批量删除消息
     */
    public function batchDelete()
    {
        $this->jump($this->logicMessage->batchDeleteMessage($this->param));
    }
    
    /**
     * 消息统计
     */
    public function statistics()
    {
        $statistics = $this->logicMessage->getMessageStatistics($this->param);
        
        $this->assign('statistics', $statistics);
        
        return $this->fetch();
    }
    
    /**
     * 清理过期消息
     */
    public function cleanExpired()
    {
        $this->jump($this->logicMessage->cleanExpiredMessages());
    }
    
    /**
     * 消息模板列表
     */
    public function template()
    {
        $where = $this->logicMessage->getTemplateWhere($this->param);
        
        $list = $this->logicMessage->getMessageTemplateList($where, true, 'id desc', DB_LIST_ROWS);
        
        $this->assign('list', $list);
        
        return $this->fetch();
    }
    
    /**
     * 添加消息模板
     */
    public function templateAdd()
    {
        IS_POST && $this->jump($this->logicMessage->addMessageTemplate($this->param));
        
        return $this->fetch();
    }
    
    /**
     * 编辑消息模板
     */
    public function templateEdit()
    {
        IS_POST && $this->jump($this->logicMessage->editMessageTemplate($this->param));
        
        $id = $this->param['id'] ?? 0;
        
        if (!$id) {
            $this->jump([RESULT_ERROR, '参数错误']);
        }
        
        $info = $this->logicMessage->getMessageTemplateInfo(['id' => $id]);
        
        if (!$info) {
            $this->jump([RESULT_ERROR, '模板不存在']);
        }
        
        $this->assign('info', $info);
        
        return $this->fetch();
    }
    
    /**
     * 删除消息模板
     */
    public function templateDelete()
    {
        $this->jump($this->logicMessage->deleteMessageTemplate($this->param));
    }

}